<%-- 
    Document   : newConversationAction
    Created on : 26.08.2014, 14:59:47
    Author     : Дмитрий
--%>

<%@page import="com.dilean.impl.model.attrval.AttrValueText"%>
<%@page import="com.dilean.api.dao.ConversationDAO"%>
<%@page import="com.dilean.impl.model.PersonImpl"%>
<%@page import="com.dilean.impl.model.ConversationImpl"%>
<%@page import="com.dilean.api.model.Conversation"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="com.dilean.api.dao.PersonDAO"%>
<%@page import="com.dilean.spring.AplicationContextHolder"%>
<%@page import="com.dilean.api.model.Person"%>
<%@page import="java.util.Collection"%>
<%@page import="java.util.Collection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
         
        <meta charset="utf-8">
        <title>Conversation Action &middot; Dilean</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="description" content="">
        <meta name="author" content="">

        
        <link href="resources/mytheme/css/bootstrap.min.css" rel="stylesheet">
        <link href="resources/mytheme/css/bootstrap.css" rel="stylesheet">
        <link href="resources/mytheme/css/simple-sidebar.css" rel="stylesheet">
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
        <link href="../resources/mytheme/css/bootstrap.min.css" rel="stylesheet">
        <script src="../resources/mytheme/js/jquery.bootstrap-duallistbox.js"></script>
        <link href="resources/mytheme/css/font-awesome.css" rel="stylesheet">
        <link href="resources/mytheme/css/font-awesome.min.css" rel="stylesheet">
    </head>
   
    <body>
        <%
            Person me = new PersonImpl();
            final int myId = 1;
            me = AplicationContextHolder.getInstance().getContext().getBean(PersonDAO.class).findByIdWithFriends(myId);
            Collection<Person> friends = me.getFriends();
            Collection<Person> friendRequests = me.getFriendRequests();
            
             int numberRequest = 0;
            for (Person fr : friendRequests) {
                if (friends.contains(fr) == false) {
                    numberRequest++;
                }
            }
            
            String convName = request.getParameter("convName");
            Collection<Person> allUsers = AplicationContextHolder.getInstance().getContext().getBean(PersonDAO.class).findAll();
            List<Integer> convUsersId = new ArrayList<Integer>();
            int size = allUsers.size();

            for (int i = 0; i < size; i++) {
                String par = "us" + i;
                String usId = request.getParameter(par);
                if(usId != null){
                    convUsersId.add(i);
                }
            }
            
            
            Conversation conversation = new ConversationImpl();
            conversation.setName(convName);
            for (Integer conUsr : convUsersId) {
                Person user = new PersonImpl();
                user = AplicationContextHolder.getInstance().getContext().getBean(PersonDAO.class).findByIdWithFriends(conUsr);
                conversation.addPerson(user);
            }
            int convId=AplicationContextHolder.getInstance().getContext().getBean(ConversationDAO.class).save(conversation).getId();
            
           

        %>
        
        
         <div id="wrapper">

            <!-- Sidebar -->
            <div id="sidebar-wrapper">
                <ul class="sidebar-nav">
                    <li class="sidebar-brand">
                        <a href="http://localhost:8080/Dilean/myPage.jsp">
                            <h2 align="centre" >Dilean</h2>
                        </a>
                    </li>
                    <li>
                        <a href="http://localhost:8080/Dilean/myPage.jsp"><i class="fa fa-home fa-fw-5x"></i>&nbsp; My Page</a>
                    </li>
                    <li>
                        <a href="http://localhost:8080/Dilean/friends.jsp?id=1"><i class="fa fa-users"></i>&nbsp; Friends </a>
                    </li>

                    <li>
                        <a href="http://localhost:8080/Dilean/allUsers.jsp?id=1"><i class="fa fa-database"></i>&nbsp; Users 
                            <%                                if (numberRequest != 0) {
                            %>
                            <span class="badge badge-info"><%=numberRequest%> </span>
                            <%
                                }
                            %>
                        </a>
                    </li>



                    <li>
                        <a href="http://localhost:8080/Dilean/conversation.jsp"><i class="fa fa-envelope"></i>&nbsp; Messages </a>
                    </li>
                    <li class="divider"></li>
                    <li>
                        <a href="http://localhost:8080/Dilean/editUser.jsp?id=1"><i class="fa fa-cog"></i>&nbsp; Settings</a>
                    </li>
                    <li>
                        <a href="#"><i class="fa fa-book"></i>&nbsp; About</a>
                    </li>
                    <li>
                        <a href="#"><i class="fa fa-sign-out"></i>&nbsp; LogOut</a>
                    </li>

                </ul>
            </div>
            <div id="page-content-wrapper">
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-lg-12">
                            <a class="btn btn-large btn-primary" href="#menu-toggle" id="menu-toggle" >
                                <i class="fa fa-user fa-3x"></i> <h4>User Menu</h4></a>
                            <br><br>

                            <div class="container">
                                <div class="well col-xs-8 col-sm-8 col-md-8 col-lg-8 col-xs-offset-2 col-sm-offset-2 col-md-offset-2 col-lg-offset-2">
                                    <!-- -->
                                    
                                    
                                    
                                    <div class="container">
                                
                                        
                                        
                                            
                                                <h1>Success</h1>
                                                <h2>You created <a href="http://localhost:8080/Dilean/chat.jsp?con=<%=convId%>">Conversation</a> with folowing users:</h2>
                                                 <ul>
                                                    <%
                                                    for(Integer convUsID : convUsersId){
                                                        Person convUs = new PersonImpl();
                                                         convUs = AplicationContextHolder.getInstance().getContext().getBean(PersonDAO.class).findByIdWithFriends(convUsID);
                                                            AttrValueText firstName = convUs.getAttributeValue(1, AttrValueText.class);
                                                            AttrValueText lastName = convUs.getAttributeValue(2, AttrValueText.class);
                                                            String fullname = "User #" + convUsID;

                                                            if ((firstName != null) && (lastName != null)) {
                                                                fullname = firstName.getValue() + " " + lastName.getValue();
                                                            } else if ((firstName == null) && (lastName != null)) {
                                                                fullname = lastName.getValue();
                                                            } else if ((firstName != null) && (lastName == null)) {
                                                                fullname = firstName.getValue();
                                                            }
                                                            
                                                            %>
                                                             <li><h3><a href="http://localhost:8080/Dilean/friend.jsp?id=<%=convUsID%>"><%=fullname%></a></h3></li>
                                                            <%
                                                    }
                                                    %>
                                                    
                                                </ul>
                                            
                                     
                                  
                            </div> 
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                               </div>
                            </div>
                        </div>
                        <script>
                            $("#menu-toggle").click(function(e) {
                                e.preventDefault();
                                $("#wrapper").toggleClass("toggled");
                            });
                            $(document).ready(function() {
                                var panels = $('.user-infos');
                                var panelsButton = $('.dropdown-user');
                                panels.hide();
                                //Click dropdown
                                panelsButton.click(function() {
                                    //get data-for attribute
                                    var dataFor = $(this).attr('data-for');
                                    var idFor = $(dataFor);
                                    //current button
                                    var currentButton = $(this);
                                    idFor.slideToggle(400, function() {
                                        //Completed slidetoggle
                                        if (idFor.is(':visible'))
                                        {
                                            currentButton.html('<i class="glyphicon glyphicon-chevron-up text-muted"></i>');
                                        }
                                        else
                                        {
                                            currentButton.html('<i class="glyphicon glyphicon-chevron-down text-muted"></i>');
                                        }
                                    })
                                });
                                $('[data-toggle="tooltip"]').tooltip();

                                $('button').click(function(e) {
                                    e.preventDefault();
                                    alert("This is a demo.\n :-)");
                                });
                            });
                        </script>
                    </div>
                </div>
            </div>
        </div>
    </body>
</html>
